package com.ttl.sbgl.loginback.controller;

import com.ttl.sbgl.loginback.entity.User;
import com.ttl.sbgl.loginback.service.UserService;
import com.ttl.sbgl.loginback.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//用户名密码登陆接口
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @PostMapping("/login")
    public Result<User> loginController(@RequestParam String name, @RequestParam String password){
        User user = userService.loginService(name,password);
        if(user != null){
            return Result.success(user,"登陆成功");
        }
        else{
            return Result.error("1001","账号或密码错误");
        }
    }
    @PostMapping("register")
    public Result<User> registerController(@RequestBody User newuser){
        User user = userService.registerService(newuser);
        if(user != null){
            return Result.success(user,"注册成功");
        }
        else{
            return Result.error("1002","注册失败,用户已存在");
        }
    }
    @PostMapping("/change-password")
    public Result<String> changePasswordController(@RequestParam String name, @RequestParam String oldPassword, @RequestParam String newPassword){
        boolean result = userService.changePasswordService(name,oldPassword,newPassword);
        if(result){
            return Result.success("修改密码成功");
        }
        else{
            return Result.error("1003","修改密码失败,用户不存在或密码错误");
        }
    }
}
